//
//  ReadSerialViewController.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/4/25.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "ReadSerialViewController.h"
#import "DetailSerialDataModel.h"
#import "AutoModel.h"
#import "ReadSerialDetailTVC.h"
@interface ReadSerialViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) NSMutableArray *arrAuto;

@property (nonatomic, retain) DetailSerialDataModel *serialDataModel;

@property (nonatomic, retain) AutoModel *autoModel;

@property (nonatomic, retain) YHYBaseTableView *tabelView;

@end

@implementation ReadSerialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataSource = [NSMutableArray array];
    self.arrAuto = [NSMutableArray array];
    [self creatView];
    [self dataHandle];
}
#pragma mark - 数据解析
- (void) dataHandle {
    NSLog(@"%@", _idNew);
    NSString *string = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/serialcontent/id=%@", self.idNew];
    [YHYAFNetTool GETNetWirhUrl:string body:nil headerFile:nil response:YHYJSON Success:^(id result) {
        
        self.serialDataModel = [[DetailSerialDataModel alloc] init];
        [_serialDataModel setValuesForKeysWithDictionary:result[@"data"]];
        [self.dataSource addObject:_serialDataModel];
        self.autoModel = _serialDataModel.authorNew;
        [self.arrAuto addObject:self.autoModel];
        NSLog(@"%@", self.arrAuto);
        
        [self.tabelView reloadData];
    } Failure:^(NSError *error) {
        NSLog(@"failure");
    }];
}

#pragma mark - 创建视图
- (void)creatView {
    
    self.view.backgroundColor = [UIColor colorWithRed:247 / 255.0 green:248 / 255.0 blue:249 / 255.0 alpha:1];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"连载";
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
    self.tabelView = [[YHYBaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.tabelView.delegate = self;
    self.tabelView.dataSource = self;
    [self.tabelView registerClass:[ReadSerialDetailTVC class] forCellReuseIdentifier:@"read"];
    [self.view addSubview:self.tabelView];
    
    [_tabelView release];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_dataSource.count != 0) {
    
        return 1;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   if (_dataSource.count != 0) {
    
        ReadSerialDetailTVC *cell = [tableView dequeueReusableCellWithIdentifier:@"read"];
        cell.serialDataModel = self.serialDataModel;
        cell.autoModel = self.autoModel;
        cell.backgroundColor = [UIColor redColor];
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_dataSource.count != 0) {
    
         DetailSerialDataModel *model = self.dataSource[indexPath.row];
        
        CGFloat Height = [ReadSerialDetailTVC heightWithTitle:model.title];
        
        return HEIGHT * 0.7 + 120 + Height;
    }
    return 0;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
